import {anyAction} from "../../untils/index"
import {Toast} from "react-vant"
const infodata = {
  data:[],
  childrenData:[],
  userarr:{},
  code:0
};
const reducer=(state=infodata,{type,payload}:anyAction)=>{
    let newState=JSON.parse(JSON.stringify(state))
    const show_Toast = (code:number,message:string) => {
        switch (code) {
            case 200:
                return Toast.success(payload.message)
            case 500:
                return Toast.fail(payload.message)
            default:
                break;
        }
    }
    switch (type) {
        
        case "GET_LIST":
            newState.data=payload
            newState.childrenData=payload[0].children
           return newState
           case "CHECK_DATA":
            newState.childrenData=newState.data[payload].children
           return newState
           case "USER_LOGIN":
            if (payload.code === 200) {
                newState.userInfo = {
                    username:payload.username,
                    password:payload.password
                }
                window.localStorage.setItem("token",JSON.stringify({
                    username:payload.username,
                    password:payload.password
                }))
            }
            
            show_Toast(payload.code,payload.message)
            newState.code = payload.code
           
            return newState
        default:
            return newState
    }
}
export default reducer